import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_shop/pages/base/base_state.dart';
import 'package:flutter_shop/pages/common/index_page/provider/index_page_provider.dart';
import 'package:flutter_shop/pages/home/home_tab_page/entity/home_content_data.dart';
import 'package:flutter_shop/pages/home/home_tab_page/entity/home_hot_data.dart';
import 'package:flutter_shop/pages/home/home_tab_page/home_tab_contract.dart';
import 'package:flutter_shop/pages/home/home_tab_page/home_tab_presenter.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/category_bar.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/floor_pics.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/floor_title_pic.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/goods_recommend.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/hot_goods.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/shop_info.dart';
import 'package:flutter_shop/pages/home/home_tab_page/widget/top_banner.dart';
import 'package:flutter_shop/res/styles.dart';
import 'package:flutter_shop/utils/utils.dart';
import 'package:flutter_shop/widget/common_app_bar.dart';
import 'package:flutter_shop/widget/load_image.dart';
import 'package:flutter_shop/widget/page_state_widget.dart';
import 'package:provider/provider.dart';

///首页tab
class HomeTabPage extends StatefulWidget {
  @override
  _HomeTabPageState createState() => _HomeTabPageState();
}

class _HomeTabPageState extends BaseState<HomeTabPage, HomeTabPresenter> with AutomaticKeepAliveClientMixin implements HomeTabView {
  ///首页数据
  HomeContentData homeContentData;

  ///火爆专区数据
  List<HomeHotData> mDatas = [];

  ///当前页
  int currentIndex = 1;

  EasyRefreshController _controller = EasyRefreshController();

  @override
  HomeTabPresenter createPresenter() => HomeTabPresenter();

  @override
  void buildComplete() {
    ///获取首页数据
    mPresenter.getHomeContent();
  }

  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);

    Widget widget = Gaps.empty;
    if (homeContentData != null) {
      //banners
      List<HomeContentDataSlide> slides = homeContentData.slides;
      //分类
      List<HomeContentDataCategory> categories = homeContentData.category;
      //广告图片
      HomeContentDataAdvertesPicture adPicture = homeContentData.advertesPicture;
      //商店信息
      HomeContentDataShopInfo shopInfo = homeContentData.shopInfo;
      //商品推荐
      List<HomeContentDataRecommand> recommands = homeContentData.recommend;
      //楼层1,2,3图片标题
      HomeContentDataFloorPic floor1Pic = homeContentData.floor1Pic;
      HomeContentDataFloorPic floor2Pic = homeContentData.floor2Pic;
      HomeContentDataFloorPic floor3Pic = homeContentData.floor3Pic;
      //楼层1,2,3图片集合
      List<HomeContentDataFloor> floor1 = homeContentData.floor1;
      List<HomeContentDataFloor> floor2 = homeContentData.floor2;
      List<HomeContentDataFloor> floor3 = homeContentData.floor3;

      widget = EasyRefresh(
          controller: _controller,
          enableControlFinishRefresh: true,
          enableControlFinishLoad: true,
          onRefresh: () async {
            setState(() {
              currentIndex = 1;
              mPresenter.getHomeContent();
              mDatas.clear();
            });
          },
          onLoad: () async {
            mPresenter.getHotGoods(currentIndex);
          },
          child: ListView(
            children: <Widget>[
              TopBanner(slides: slides), //顶部banner
              Gaps.v_gap_5,
              CategoryBar(categories: categories), //分类导航
              Gaps.v_gap_5,
              Image.network(adPicture.pictureAddress), //广告图片
              ShopInfo(image: shopInfo.leaderImage, phone: shopInfo.leaderPhone), //商店信息
              Gaps.v_gap_5,
              GoodsRecommend(recommends: recommands), //商品推荐
              FloorTitlePic(titlePic: floor1Pic), //楼层1
              FloorPics(floorPics: floor1),
              FloorTitlePic(titlePic: floor2Pic), //楼层2
              FloorPics(floorPics: floor2),
              FloorTitlePic(titlePic: floor3Pic), //楼层3
              FloorPics(floorPics: floor3),
              HotGoods(list: mDatas), //火爆专区
            ],
          ));
    }

    String currentCity = Provider.of<IndexPageProvider>(context).currentCity;

    return Scaffold(
      appBar: CommonAppBar("首页", showBack: false, rightText: Utils.strIsEmpty(currentCity) ? "定位中..." : currentCity),
      body: PageStateWidget(
        controller: pageStateController,
        retryClick: () {
          pageStateController.showLoading();
          mPresenter.getHomeContent();
        },
        child: widget,
      ),
    );
  }

  @override
  void showHotData(List<HomeHotData> list) {
    setState(() {
      if (currentIndex == 1) {
        mDatas.clear();
      }
      mDatas.addAll(list);
      currentIndex++;
      _controller.finishLoad(success: true, noMore: list.isEmpty);
    });
  }

  @override
  void showPageData(HomeContentData data) {
    _controller.finishRefresh(success: true);
    pageStateController.showSuccess();
    setState(() {
      homeContentData = data;
    });
  }

  @override
  void showError(String str) {
    _controller.finishRefresh(success: true);
    _controller.finishLoad(success: false);
    pageStateController.showError(str);
    showToast(str);
  }
}
